// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2024-12-03

package app

type (
	// PayloadStatus
	// is a type name use to remark producer message status.
	PayloadStatus     int
	PayloadStatusText string
)

const (
	PayloadStatusCreated PayloadStatus = iota
	PayloadStatusSucceed
	PayloadStatusFailed
	PayloadStatusWaiting
	PayloadStatusProcessing
	PayloadStatusIgnored
)

const (
	PayloadStatusTextCreated    PayloadStatusText = "created"
	PayloadStatusTextSucceed    PayloadStatusText = "succeed"
	PayloadStatusTextFailed     PayloadStatusText = "failed"
	PayloadStatusTextWaiting    PayloadStatusText = "waiting"
	PayloadStatusTextProcessing PayloadStatusText = "processing"
	PayloadStatusTextIgnored    PayloadStatusText = "ignored"
)

var (
	payloadStatusText = map[PayloadStatus]PayloadStatusText{
		PayloadStatusCreated:    PayloadStatusTextCreated,
		PayloadStatusSucceed:    PayloadStatusTextSucceed,
		PayloadStatusFailed:     PayloadStatusTextFailed,
		PayloadStatusWaiting:    PayloadStatusTextWaiting,
		PayloadStatusProcessing: PayloadStatusTextProcessing,
		PayloadStatusIgnored:    PayloadStatusTextIgnored,
	}
	payloadStatusTextUnknown StatusText = ""
)

func (o PayloadStatus) Created() bool    { return o == PayloadStatusCreated }
func (o PayloadStatus) Failed() bool     { return o == PayloadStatusFailed }
func (o PayloadStatus) Ignored() bool    { return o == PayloadStatusIgnored }
func (o PayloadStatus) Processing() bool { return o == PayloadStatusProcessing }
func (o PayloadStatus) Succeed() bool    { return o == PayloadStatusSucceed }
func (o PayloadStatus) Waiting() bool    { return o == PayloadStatusWaiting }

func (o PayloadStatus) Text() string {
	if s, ok := payloadStatusText[o]; ok {
		return s.String()
	}
	return payloadStatusTextUnknown.String()
}

func (o PayloadStatusText) Can() (status PayloadStatus, has bool) {
	switch o {
	case PayloadStatusTextCreated:
		return PayloadStatusCreated, true
	case PayloadStatusTextSucceed:
		return PayloadStatusSucceed, true
	case PayloadStatusTextFailed:
		return PayloadStatusFailed, true
	case PayloadStatusTextWaiting:
		return PayloadStatusWaiting, true
	case PayloadStatusTextProcessing:
		return PayloadStatusProcessing, true
	case PayloadStatusTextIgnored:
		return PayloadStatusIgnored, true
	}
	return
}

func (o PayloadStatusText) String() string {
	return string(o)
}
